package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.beans.ArticleDetailEntity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.HandleString;

/**
 * Created by YY on 2015/9/5.
 */
public class GetOneArticleRequest extends ByhhAndroidRequest {

    public GetOneArticleRequest(int requestMethod, String url, Map<String, String> params,
                                RequestCallback callback, Response.ErrorListener listener) {
        super(requestMethod, url, params, callback, listener);

    }

    public static void execute(Context context, Map<String, String> params, RequestCallback requestCallback) {
        executeSuper(context, RequestType.ARTICLE_ONE, params, requestCallback);
    }

    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> result) {
        final String TAG_ARTICLE = "article";
        final String TAG_TITLE = "title";
        final String TAG_OWNER = "owner";
        final String TAG_CONTENT = "content";
        String TAG_FILENAME = "filename";

        ArticleDetailEntity articleDetailEntity = null;
        List<ArticleDetailEntity> articleList = new ArrayList<>();

        try {
            int event = parser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();
                        if (tagName.equals(TAG_ARTICLE)) {
                            articleDetailEntity = new ArticleDetailEntity();
                        } else if (articleDetailEntity != null && tagName.equals(TAG_TITLE)) {
                            articleDetailEntity.setArticleTitle(parser.nextText());
                        } else if (articleDetailEntity != null && tagName.equals(TAG_OWNER)) {
                            articleDetailEntity.setAuthorID(parser.nextText());
                        } else if (articleDetailEntity != null && tagName.equals(TAG_CONTENT)) {
                            String content = parser.nextText();
                            String[] strings = HandleString.parsePostContent(content);
                            articleDetailEntity.setArticleDetail(strings[0]);
                            articleDetailEntity.setAuthorName(strings[1]);
                            articleDetailEntity.setPostTime(strings[2]);
                        } else if (articleDetailEntity != null && tagName.equals(TAG_FILENAME)) {
                            articleDetailEntity.setFileName(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTag = parser.getName();
                        if (endTag.equals(TAG_ARTICLE)) {
                            articleList.add(articleDetailEntity);
                            articleDetailEntity = null;
                        }
                        break;
                    default:
                        break;
                }
                event = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.put(ConstantsData.ARTICLE_LIST, articleList);
    }
}
